<?php
require_once 'entity/ChallengeRunner.php';
class Tool{

	//------------------------------------------ CONSTANT
	const HOUR="HOUR";
	const MIN="MIN";
	const SEC="SEC";

	//------------------------------------------ PRIVATE ATTRIBUTES
	private $dbUtils=null;

	//------------------------------------------ CONSTRUCTOR
	function Tool($dbUtils){
		$this->dbUtils=$dbUtils;
	}
	//------------------------------------------ PUBLIC METHODS
	function teamsplit($string){
		return preg_split("#%#",$string);
	}
	function toString($string){
		return preg_replace("#%#"," - ",$string);
	}

	function getFormation($tab){
		$lastName1=$tab['nom1'];
		$lastName2=$tab['nom2'];

		$firstName1=$tab['prenom1'];
		$firstName2=$tab['prenom2'];
		$formation="";
		if(Config::team){
			$formation="[".$lastName1." ".$firstName1."] - [".$lastName2." ".$firstName2."]";
		}else{
			$formation=$lastName1." ".$firstName1;
		}
		return $formation;

	}
	function teamConcat($member1,$member2){
		return $member1."%".$member2;
	}
	function listQuery($tabRunners,$title,$course){
		?>
<table class="principale">
	<tr>
		<th colspan="7"><h2>
				<?php echo $title;?>
			</h2></th>
	</tr>
	<tr>

		<th><?php If (Config::team) {echo "Formation";}else{echo "Nom prénom";}?></th>
		<th>Dossard</th>
		<th>Temps</th>
		<?php if ("10 Derniéres arrivées"!=$title) {?>
		<th>Général</th>
		<th>Femme</th>
		<th>Homme</th>
		<?php if(Config::team) {?><th>Mixte</th><?php }?>
		<?php }?>
	</tr>

	<?php
	if(sizeof($tabRunners)>0){
			$time=$this->dbUtils->getTime($course);

			$cptF=1;
			$cptH=1;
			$cptMixte=1;
			for($i=0;$i<sizeof($tabRunners);$i++){?>
	<tr class="list" style="font-size: 12px">

		<td class="listing"><?php echo($this->getFormation($tabRunners[$i])); ?>
		</td>
		<td class="listing" align="center"><?php echo($tabRunners[$i]['dossard']); ?>
		</td>
		<td class="listing" align="center"><?php echo($this->getTimeDif($time,$tabRunners[$i]['temp'])); ?>
		</td>
		<?php if ("10 Derniéres arrivées"!=$title) {?>
		<td class="listing" align="center"><?php echo($i+1); ?></td>
		<td class="listing" align="center"><?php if($tabRunners[$i]['sexe1']=="F" && $tabRunners[$i]['sexe2']=="F"){
			echo $cptF++;
		}?>
		</td>
		<td class="listing" align="center"><?php if($tabRunners[$i]['sexe1']=="M" && $tabRunners[$i]['sexe2']=="M"){
			echo $cptH++;
		}?>
		</td>
		<?php if(Config::team) {?>
		<td class="listing" align="center"><?php 
		if(($tabRunners[$i]['sexe1'].$tabRunners[$i]['sexe2']=="MF") || ($tabRunners[$i]['sexe1'].$tabRunners[$i]['sexe2']=="FM")){
						echo $cptMixte++;
					}?>
		<?php }?>
		</td>
		<?php }?>
	</tr>

	<?php }
			} ?>
</table>
<?php
	}

	function listRunnersLeft($tabRunners,$title){

		if($tabRunners!=0){
			?>
<table class="principale">
	<tr>
		<th colspan="5"><h2>
				<?php echo $title?>
			</h2></th>
	</tr>
	<tr>
		<th>N°</th>
		<th><?php If (Config::team) {echo "Formation";}else{echo "Nom prénom";}?></th>
		<th>Dossard</th>
	</tr>

	<?php
	$cptF=1;
	for($i=0;$i<sizeof($tabRunners);$i++){

		?>
	<tr class="list">
		<td class="listing" style="text-align: center;"><?php echo($i+1); ?></td>
		<td class="listing"><?php echo( $this->getFormation($tabRunners[$i])); ?>
		</td>
		<td class="listing" align="center"><?php echo($tabRunners[$i]['dossard']); ?>
		</td>
		</td>
	</tr>

	<?php } ?>
</table>
<?php

		}


	}

	function getRunner($nom, $prenom,$chalengeRunners){


		//echo "looking for ".$nom." : ".$prenom."<br>";
		$chalengeRunner=null;
		$found=false;
		for($i=0;$i<sizeof($chalengeRunners) && !$found;$i++){
			$found=$chalengeRunners[$i]->getNom()==$nom && $chalengeRunners[$i]->getPrenom()==$prenom;
		}
		if($found){

			$chalengeRunner=$chalengeRunners[--$i];
			//echo "FOUND :<br> ";
			//$chalengeRunner->toString();
			//echo "<br>";
			//echo "<br>";
		}
		return $chalengeRunner;
	}


	function buildList($tabRunners){

		$positionNob=1;
		$positionRos=1;
		$positionLes=1;

		$chalengeRunnerOne=null;
		$chalengeRunnerTwo=null;
		$chalengeRunners =null;

		for($i=0;$i<sizeof($tabRunners);$i++){

			$nom1=$tabRunners[$i]['nom1'];
			$prenom1=$tabRunners[$i]['prenom1'];

			$nom2=$tabRunners[$i]['nom2'];
			$prenom2=$tabRunners[$i]['prenom2'];


			$chalengeRunnerOne=$this->getRunner($nom1, $prenom1,$chalengeRunners);
			$chalengeRunnerTwo=$this->getRunner($nom2, $prenom2,$chalengeRunners);


			$course=$tabRunners[$i]['course'];

			if($chalengeRunnerOne==null){
				$chalengeRunnerOne=new ChallengeRunner($nom1, $prenom1);
				$chalengeRunners[sizeof($chalengeRunners)]=$chalengeRunnerOne;
			}
			if($chalengeRunnerTwo==null){
				$chalengeRunnerTwo=new ChallengeRunner($nom2, $prenom2);
				$chalengeRunners[sizeof($chalengeRunners)]=$chalengeRunnerTwo;
			}
			if($course=="NOBRESSART"){

				$chalengeRunnerOne->setNobPosition($positionNob);
				$chalengeRunnerTwo->setNobPosition($positionNob);
				$positionNob++;
			}
			if($course=="ROSSIGNOL"){

				$chalengeRunnerOne->setRosPosition($positionRos);
				$chalengeRunnerTwo->setRosPosition($positionRos);
				$positionRos++;
			}
			if($course=="LES_FOSSES"){

				$chalengeRunnerOne->setLesPosition($positionLes);
				$chalengeRunnerTwo->setLesPosition($positionLes);
				$positionLes++;
			}

		}

		for($i=0;$i<sizeof($chalengeRunners);$i++){
			$currentRunner=$chalengeRunners[$i];
			$points=$currentRunner->buildChallengePoints();
		}

		$this->bubblesort($chalengeRunners,sizeof($chalengeRunners));

		return $chalengeRunners ;
	}

	function bubblesort(&$ptab, $n)	{
		$i = null; $j = null; $temp = null;

		for ($i = 0; $i < ($n - 1); $i++)
		{
			for ($j = ($i + 1); $j < $n; $j++)
			{
				if ($ptab[$j]->getChallengeTotal() > $ptab[$i]->getChallengeTotal())
				{
					$temp = $ptab[$i];
					$ptab[$i] = $ptab[$j];
					$ptab[$j] = $temp;
				}
			}
		}
	}
	function array_sort($array, $on, $order=SORT_ASC){
	    $new_array = array();
	    $sortable_array = array();

	    if (count($array) > 0) {
	        foreach ($array as $k => $v) {
	            if (is_array($v)) {
	                foreach ($v as $k2 => $v2) {
	                    if ($k2 == $on) {
	                        $sortable_array[$k] = $v2;
	                    }
	                }
	            } else {
	                $sortable_array[$k] = $v;
	            }
	        }

	        switch ($order) {
	        	case SORT_ASC:
	        		asort($sortable_array);
	        		break;
	        	case SORT_DESC:
	        		arsort($sortable_array);
	        		break;
	        }

	        foreach ($sortable_array as $k => $v) {
	            $new_array[$k] = $array[$k];
	        }
	    }

	    return $new_array;
	}

	function getTimeDif($startTime,$endTime,$part=""){
		$hourStart=substr($startTime,11,2);
		$minStart=substr($startTime,14,2);
		$secStart=substr($startTime,17,2);

		$monthStart=substr($startTime,5,2);
		$dayStart=substr($startTime,8,2);
		$yearStart=substr($startTime,0,4);

		//echo($startTime."=".$hourStart."-".$minStart."-".$secStart."-".$monthStart."-".$dayStart."-".$yearStart);

		$startStamp = mktime ($hourStart,$minStart,$secStart,$monthStart,$dayStart,$yearStart);




		$hourend=substr($endTime,11,2);
		$minend=substr($endTime,14,2);
		$secend=substr($endTime,17,2);

		$monthend=substr($endTime,5,2);
		$dayend=substr($endTime,8,2);
		$yearend=substr($endTime,0,4);

		$endStamp = mktime ($hourend,$minend,$secend,$monthend,$dayend,$yearend);

		$sec=$endStamp-$startStamp;

		$hour="00";
		$min="00";
		if($sec>=60){
			$min=floor($sec/60);
			$sec=$sec-($min*60);
			if($min>=60){
				$hour=floor($min/60);
				$min=$min-($hour*60);

			}
		}

		if(strlen($min)<2){
			$min="0".$min;
		}

		if(strlen($hour)<2){
			$hour="0".$hour;
		}

		if(strlen($sec)<2){
			$sec="0".$sec;
		}
		if ($part!=""){
			if(Tool::HOUR==$part){
				return $hour;
			}else if(Tool::MIN==$part){
				return $min;
			}else if(Tool::SEC==$part){
				return $sec;
			}
		}
		return $hour.":".$min.":".$sec;

	}


}
?>